package linwen.wang.mapper;


import org.apache.commons.lang.time.StopWatch;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@SpringBootTest
@RunWith(SpringRunner.class)
public class MybatisInsert {
    @Autowired
    MybatisMapper mapper;

    @Autowired
    ThreadPoolExecutor threadPoolExecutor;

    @Test
    public void test1(){
        // 百万数据，会出现内存溢出
        List<BatchVO> list = new ArrayList();
        for (int i = 0; i < 1000000; i++) {
            BatchVO batchVO = new BatchVO();
            batchVO.setName("张三"+(int)(Math.random()*10000));
            batchVO.setGender(((int)(Math.random()*10)%2==0?"男":"女"));
            batchVO.setNumber((int)Math.random()*10000);
            batchVO.setAddress("广东深圳");
            batchVO.setCreatedBy("李四"+(int)(Math.random()*10000));
            batchVO.setLastUpdatedBy("王武"+(int)(Math.random()*10000));
            batchVO.setDelFlag("N");
            list.add(batchVO);
        }
        mapper.addList(list);
    }

    @Test
    public void test2(){// 耗时严重
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<BatchVO> list = new ArrayList();
        for (int i = 0; i < 1000000; i++) {
            BatchVO batchVO = new BatchVO();
            batchVO.setName("张三"+(int)(Math.random()*10000));
            batchVO.setGender(((int)(Math.random()*10)%2==0?"男":"女"));
            batchVO.setNumber((int)Math.random()*10000);
            batchVO.setAddress("广东深圳");
            batchVO.setCreatedBy("李四"+(int)(Math.random()*10000));
            batchVO.setLastUpdatedBy("王武"+(int)(Math.random()*10000));
            batchVO.setDelFlag("N");
            list.add(batchVO);
        }
        System.out.println("初始化数据耗时  "+stopWatch.getTime());
        stopWatch.reset();
        stopWatch.start();
        List<BatchVO> batchVOS;
        int count = 0;
        int batchCount = 10000;
        while (list.size()>count*batchCount){
            if(list.size()>batchCount){
                batchVOS= list.subList(count*batchCount, (count+1)*batchCount);
                mapper.addList(batchVOS);
                count++;
            }else{
                batchVOS = list.subList(count*batchCount, list.size());
                mapper.addList(batchVOS);
                count++;
            }
        }
        System.out.println("插入耗时  "+stopWatch.getTime());
    }

    @Test
    public void test3() throws InterruptedException {// 配置多线程之后确实可以快一点
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<BatchVO> list = new ArrayList();
        for (int i = 0; i < 1000000; i++) {
            BatchVO batchVO = new BatchVO();
            batchVO.setName("张三"+(int)(Math.random()*10000));
            batchVO.setGender(((int)(Math.random()*10)%2==0?"男":"女"));
            batchVO.setNumber((int)Math.random()*10000);
            batchVO.setAddress("广东深圳");
            batchVO.setCreatedBy("李四"+(int)(Math.random()*10000));
            batchVO.setLastUpdatedBy("王武"+(int)(Math.random()*10000));
            batchVO.setDelFlag("N");
            list.add(batchVO);
        }
        System.out.println("初始化数据耗时  "+stopWatch.getTime());
        stopWatch.reset();
        stopWatch.start();
        List<BatchVO> batchVOS;
        int count = 0;
        int batchCount = 1000;
        while (list.size()>count*batchCount){
            if(list.size()>batchCount){
                batchVOS= list.subList(count*batchCount, (count+1)*batchCount);
                List<BatchVO> finalBatchVOS = batchVOS;
                threadPoolExecutor.execute(()->{
                    mapper.addList(finalBatchVOS);
                });
                count++;
            }else{
                batchVOS = list.subList(count*batchCount, list.size());
                List<BatchVO> finalBatchVOS1 = batchVOS;
                threadPoolExecutor.submit(()->{
                    mapper.addList(finalBatchVOS1);
                });
                count++;
            }
        }
        threadPoolExecutor.shutdown();
        // 等待线程执行完任务之后再退出
        System.out.println(threadPoolExecutor.awaitTermination(2, TimeUnit.MINUTES));
        System.out.println("插入耗时  "+stopWatch.getTime());
    }

    @Test
    public void test4(){
        // 查看当前机器cup数量
        System.out.println(Runtime.getRuntime().availableProcessors());
    }
}
